<?php

namespace Api\Model;

use Api\Model\BaseModel;

/**
 * 
 * @author star7th      
 */
class SubscriptionModel extends BaseModel
{

  public function addSub($uid, $object_id, $object_type, $action_type)
  {

    $uid = intval($uid);
    $object_id = intval($object_id);
    // 检测是否已经存在订阅了（数组条件优先）
    $res = $this->where(array(
      'uid' => $uid,
      'object_id' => $object_id,
      'object_type' => $object_type,
      'action_type' => $action_type
    ))->find();
    if (!$res) {
      $res = $this->add(array(
        "uid" => $uid,
        "object_id" => $object_id,
        "object_type" => $object_type,
        "action_type" => $action_type,
        "sub_time" => date("Y-m-d H:i:s")
      ));
    }
    return $res;
  }

  public function deleteSub($uid, $object_id, $object_type, $action_type)
  {
    $uid = intval($uid);
    $object_id = intval($object_id);
    // 删除订阅（数组条件优先）
    $res = $this->where(array(
      'uid' => $uid,
      'object_id' => $object_id,
      'object_type' => $object_type,
      'action_type' => $action_type
    ))->delete();
    return $res;
  }

  public function getListByObjectId($object_id, $object_type, $action_type)
  {

    $object_id = intval($object_id);
    return $this->where(array(
      'object_id' => $object_id,
      'object_type' => $object_type,
      'action_type' => $action_type
    ))->select();
  }
}
